﻿
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Windows;
    using System.Windows.Resources;
namespace WCYCReader.Utility
{
    public class FIleUtility
    {
        public static long AvailableFreeSpace()
        {
            return IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace;
        }

        public static void CreateDirectory(string dir)
        {
            if (((dir != null) && (dir.Length != 0)) && !DirectoryExists(dir))
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.CreateDirectory(dir);
                }
            }
        }

        public static void DeleteDirectory(string dir)
        {
            if (((dir != null) && (dir.Length != 0)) && DirectoryExists(dir))
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.DeleteDirectory(dir);
                }
            }
        }

        public static bool DeleteFile(string strPath)
        {
            if (IsFileExist(strPath))
            {
                try
                {
                    IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(strPath);
                    return true;
                }
                catch (Exception exception)
                {
                    LogUtility.Log(exception.ToString(), new object[0]);
                    return false;
                }
            }
            return true;
        }

        public static bool DirectoryExists(string dir)
        {
            if ((dir != null) && (dir.Length != 0))
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    return file.DirectoryExists(dir);
                }
            }
            return false;
        }

        public static void EnsureDirectoryExist(string dir)
        {
            CreateDirectory(dir);
        }

        public static string GetExtension(string path)
        {
            if (path != null)
            {
                int startIndex = path.LastIndexOf(".");
                if (startIndex >= 0)
                {
                    return path.Substring(startIndex);
                }
            }
            return null;
        }

        public static long GetFileLength(string strFileName)
        {
            long length;
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        length = stream.Length;
                    }
                }
            }
            catch
            {
                length = 0L;
            }
            return length;
        }

        public static DateTimeOffset GetLastWriteTime(string path)
        {
            return IsolatedStorageFile.GetUserStoreForApplication().GetLastWriteTime(path);
        }

        public static bool IncreaseIsolatedStorageSpace(long quotaSizeDemand)
        {
            bool flag = false;
            IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
            long availableFreeSpace = userStoreForApplication.AvailableFreeSpace;
            if (quotaSizeDemand <= availableFreeSpace)
            {
                return flag;
            }
            if (!userStoreForApplication.IncreaseQuotaTo(userStoreForApplication.Quota + quotaSizeDemand))
            {
                return false;
            }
            return true;
        }

        public static bool IsFileExist(string strPath)
        {
            return IsolatedStorageFile.GetUserStoreForApplication().FileExists(strPath);
        }

        public static void MoveFile(string sourceFileName, string destinationFileName)
        {
            IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
            try
            {
                if (userStoreForApplication.FileExists(sourceFileName))
                {
                    if (userStoreForApplication.FileExists(destinationFileName))
                    {
                        userStoreForApplication.DeleteFile(destinationFileName);
                    }
                    userStoreForApplication.MoveFile(sourceFileName, destinationFileName);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (userStoreForApplication != null)
                {
                    userStoreForApplication.Dispose();
                }
            }
        }

        public static long Quota()
        {
            return IsolatedStorageFile.GetUserStoreForApplication().Quota;
        }

        public static string ReadFile(string fileName)
        {
            StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
            if (resourceStream != null)
            {
                using (StreamReader reader = new StreamReader(resourceStream.Stream))
                {
                    return reader.ReadToEnd();
                }
            }
            return null;
        }

        public static string SeekFile(string fileName, int index, int count)
        {
            StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
            if (resourceStream != null)
            {
                using (StreamReader reader = new StreamReader(resourceStream.Stream))
                {
                    char[] buffer = new char[count + 1];
                    reader.ReadBlock(buffer, index, count);
                    return new string(buffer);
                }
            }
            return null;
        }

        public static void WriteFile(string szFrom, string szTo)
        {
            StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri(szFrom, UriKind.Relative));
            if (resourceStream != null)
            {
                IsolatedStorageFileStream output = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(szTo, FileMode.Create);
                if (output != null)
                {
                    BinaryReader reader = new BinaryReader(resourceStream.Stream);
                    BinaryWriter writer = new BinaryWriter(output);
                    for (byte[] buffer = reader.ReadBytes(0x400); buffer.Length > 0; buffer = reader.ReadBytes(0x400))
                    {
                        writer.Write(buffer);
                    }
                    writer.Close();
                    reader.Close();
                }
                output.Close();
            }
        }
    }
}

